package com.controller;



import com.pojo.Notice;
import com.pojo.Page;
import com.pojo.User;
import com.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @RequestMapping("/noticelist")
    @ResponseBody
    public List<Notice> queryNoticeAll(){
        List<Notice>list=   noticeService.queryNotice();
        return list;
    }
    @RequestMapping("/queryall1")
    @ResponseBody
    public Page queryall(int pageindex){
        Page page=new Page(5,pageindex);
        List<Notice>list=noticeService.queryFen(page.getStartIndex(),page.getEndIndex());
        int row=noticeService.countAll(null,null);
        page.setResult(list,row);
        return page;
    }
    @RequestMapping("/querytitle")
    @ResponseBody
    public Page querytitle(int pageindex,String title,String user_id){
        Page page=new Page(5,pageindex);
        List<Notice>list=noticeService.queryTitle(page.getStartIndex(),page.getEndIndex(),title,user_id);
        int row=noticeService.countAll(title,user_id);
        page.setResult(list,row);
        return page;
    }
   /* @RequestMapping("/queryid")
    @ResponseBody
    public Page queryid(int pageindex,String user_id){
        Page page=new Page(5,pageindex);
        List<Notice>list=noticeService.queryId(page.getStartIndex(),page.getEndIndex(),user_id);
        int row=noticeService.countId(user_id);
        page.setResult(list,row);
        return page;
    }*/

    @RequestMapping("/addnotice")
    @ResponseBody
    public String addNotice(@RequestBody Notice notice){
        int row=   noticeService.addNotice(notice);
        if(row>0){
            return "{\"fig\":\"success\"}";
        }else {
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/updatenotice")
    @ResponseBody
    public String updateNotice(@RequestBody Notice notice){
        int row=   noticeService.updateNotice(notice);
        if(row>0){
            return "{\"fig\":\"success\"}";
        }else {
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/querynotice")
    @ResponseBody
    public List<Notice> queryNoticeByTitle(String title){
        List<Notice>list=   noticeService.queryNoticeByTitle(title);
        return list;
    }
    @RequestMapping("/deletenotice")
    @ResponseBody
    public String deleteNotice(int id){
        int row = noticeService.deleteNotice(id);
        if(row>0){
            return "{\"fig\":\"success\"}";
        }else {
            return "{\"fig\":\"error\"}";
        }
    }
}
